﻿
namespace MyFurion.Application
{
    /// <summary>
    /// 字典仓储
    /// </summary>
    public class DictRepository:BaseRepository<DictInfo>,ITransient
    {
        /// <summary>
        /// 查询条件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private Expression<Func<DictInfo, bool>> GetCodition(DictInfoPage input)
        {
            var exp = Expressionable.Create<DictInfo>();
            exp.AndIF(!string.IsNullOrWhiteSpace(input.DictCode), it => it.DictCode.Contains(input.DictCode));
            exp.AndIF(!string.IsNullOrWhiteSpace(input.DictName), it => it.DictName.Contains(input.DictName));
            exp.AndIF(input.Status.HasValue, it => it.Status == input.Status.Value);
            return exp.ToExpression();
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<DictInfo>> GetPageList(DictInfoPage input)
        {
            var whereExp = GetCodition(input);
            return await GetPageListAsync(whereExp, input.PageIndex, input.PageSize, it => it.SortNum,OrderByType.Asc);
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<List<DictInfo>> GetDataList(DictInfoPage input)
        {
            var whereExp = GetCodition(input);
            return await GetList(whereExp, it => it.SortNum, OrderByType.Asc);
        }
        /// <summary>
        /// 数据是否存在
        /// </summary>
        /// <param name="dictCode"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DataExists(string dictCode, long? id = null)
        {
            return await Context.Queryable<DictInfo>()
                .Where(it => it.DictCode == dictCode)
                .WhereIF(id.HasValue, it => it.Id != id.Value)
                .AnyAsync();
        }
    }
}
